//
//  LXRegisterViewController.m
//  cat
//
//  Created by imera on 16/3/17.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXRegisterViewController.h"
#import "LXNavBarView.h"
#import "LXInputView.h"
#import "UIImage+Lx.h"
#import "MBProgressHUD+MJ.h"
#import "AFNetworking.h"

@interface LXRegisterViewController() <LXNavBarViewDelegate>
/** 用户名 */
@property (nonatomic, weak) LXInputView *userNameInputView;
/** 新密码 */
@property (nonatomic, weak) LXInputView *nePwdInputView;
/** 确认密码 */
@property (nonatomic, weak) LXInputView *confirmPwdInputView;
/** 注册按钮 */
@property (nonatomic, weak) UIButton *registerBtn;
@end

@implementation LXRegisterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initNavBar];

    [self initSubViews];
}

#pragma mark 自定义导航条
- (void)initNavBar
{
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    navBarView.leftTitle = @"注册";
    navBarView.myDelegate = self;
    [self.view addSubview:navBarView];
}

#pragma mark -  初始化子控件
- (void)initSubViews
{
    /** 用户名 */
    LXInputView *userNameInputView = [LXInputView initWithIcon:@"user" placeholder:@"请输入你的邮箱"];
    [self.view addSubview:userNameInputView];
    self.userNameInputView = userNameInputView;

    CGFloat userNameInputViewX = 10;
    CGFloat userNameInputViewY = 80;
    CGFloat userNameInputViewW = self.view.frame.size.width - userNameInputViewX * 2;
    CGFloat userNameInputViewH = 40;
    userNameInputView.frame = CGRectMake(userNameInputViewX, userNameInputViewY, userNameInputViewW, userNameInputViewH);
    
    /** 新密码 */
    LXInputView *newPwdInputView = [LXInputView initWithIcon:@"pwd" placeholder:@"密码"];
    newPwdInputView.isPwd = YES;
    [self.view addSubview:newPwdInputView];
    self.nePwdInputView = newPwdInputView;
    
    CGFloat newPwdInputViewX = userNameInputViewX;
    CGFloat newPwdInputViewY = CGRectGetMaxY(userNameInputView.frame) + 10;
    CGFloat newPwdInputViewW = userNameInputViewW;
    CGFloat newPwdInputViewH = userNameInputViewH;
    newPwdInputView.frame = CGRectMake(newPwdInputViewX, newPwdInputViewY, newPwdInputViewW, newPwdInputViewH);
    
    /** 确认密码 */
    LXInputView *confirmPwdInputView = [LXInputView initWithIcon:@"pwd" placeholder:@"确认密码"];
    confirmPwdInputView.isPwd = YES;
    [self.view addSubview:confirmPwdInputView];
    self.confirmPwdInputView = confirmPwdInputView;
    
    CGFloat confirmPwdInputViewX = userNameInputViewX;
    CGFloat confirmPwdInputViewY = CGRectGetMaxY(newPwdInputView.frame) + 10;
    CGFloat confirmPwdInputViewW = userNameInputViewW;
    CGFloat confirmPwdInputViewH = userNameInputViewH;
    confirmPwdInputView.frame = CGRectMake(confirmPwdInputViewX, confirmPwdInputViewY, confirmPwdInputViewW, confirmPwdInputViewH);
    
    /** 注册按钮 */
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setBackgroundImage:[UIImage resizeWithName:@"blue"] forState:UIControlStateNormal];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(userRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    self.registerBtn = registerBtn;
    
    CGFloat registerBtnX = userNameInputViewX;
    CGFloat registerBtnY = CGRectGetMaxY(confirmPwdInputView.frame) + 10;
    CGFloat registerBtnW = userNameInputViewW;
    CGFloat registerBtnH = userNameInputViewH;
    registerBtn.frame = CGRectMake(registerBtnX, registerBtnY, registerBtnW, registerBtnH);
}

#pragma mark - 用户注册
- (void)userRegister
{
    if ([self.userNameInputView.textField.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"用户名不能为空"];
        return;
    }
    if ([self.nePwdInputView.textField.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"密码不能为空"];
        return;
    }
    if ([self.confirmPwdInputView.textField.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"确认密码不能为空"];
        return;
    }
    if (![self isValidateEmail:self.userNameInputView.textField.text]) {
        [MBProgressHUD showError:@"邮箱的格式不正确"];
        return;
    }
    if (![self.nePwdInputView.textField.text isEqualToString:self.confirmPwdInputView.textField.text]) {
        [MBProgressHUD showError:@"两次输入的密码不一致"];
        return;
    }
    
    //注册
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSString *url = @"http://cat666.com/cat666-interface/index.php/index/register";
    [mgr POST:url parameters:@{@"user":self.userNameInputView.textField.text,@"password":self.nePwdInputView.textField.text} success:^(AFHTTPRequestOperation *operation, id response) {
        NSDictionary *dict = response;
        if (dict.allKeys.count == 1) {
            if([dict[@"exist"] integerValue] == 1){
                [MBProgressHUD showError:@"该邮箱已经被注册"];
            }else{
                [MBProgressHUD showError:@"注册失败"];
            }
        }else{
            [MBProgressHUD showSuccess:@"注册成功"];
            [[NSUserDefaults standardUserDefaults]setObject:self.userNameInputView.textField.text forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults]setObject:dict[@"id"] forKey:@"userId"];
            [[NSUserDefaults standardUserDefaults]setObject:self.nePwdInputView.textField.text forKey:@"password"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        [MBProgressHUD showError:@"您的网络开小差了..."];
    }];
    
}

#pragma mark - 自定义导航栏代理方法
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 验证是否为邮箱格式
- (BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

@end
